package com.wwtech.epp.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.content.FileProvider;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import io.rong.imkit.RongContext;

@SuppressWarnings("unused")
public class FileUtils {
	
	public static String SDPATH = Environment.getExternalStorageDirectory()
			+ "/Photo_LJ/";

	public static String saveBitmap(Bitmap bm, String picName) {
		try {
			if (!isFileExist("")) {
				File tempf = createSDDir("");
			}
			File f = new File(SDPATH, picName + ".JPG"); 
			if (f.exists()) {
				f.delete();
			}
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
			return f.getPath();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static File createSDDir(String dirName) throws IOException {
		File dir = new File(SDPATH + dirName);
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {

			System.out.println("createSDDir:" + dir.getAbsolutePath());
			System.out.println("createSDDir:" + dir.mkdir());
		}
		return dir;
	}

	public static boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		file.isFile();
		return file.exists();
	}
	
	public static void delFile(String fileName){
		File file = new File(SDPATH + fileName);
		if(file.isFile()){
			file.delete();
        }
		file.exists();
	}

	public static void deleteDir() {
		File dir = new File(SDPATH);
		if (dir == null || !dir.exists() || !dir.isDirectory())
			return;
		
		for (File file : dir.listFiles()) {
			if (file.isFile())
				file.delete(); 
			else if (file.isDirectory())
				deleteDir(); 
		}
		dir.delete();
	}

	public static boolean fileIsExists(String path) {
		try {
			File f = new File(path);
			if (!f.exists()) {
				return false;
			}
		} catch (Exception e) {

			return false;
		}
		return true;
	}

	public static Intent getOpenFileIntent(String fileName, String fileSavePath, Context context) {
		Intent intent = new Intent("android.intent.action.VIEW");
		String type = null;
		if(checkSuffix(fileName, RongContext.getInstance().getResources().getStringArray(io.rong.imkit.R.array.rc_image_file_suffix))) {
			intent.addCategory("android.intent.category.DEFAULT");
			intent.addFlags(268435456);
			type = "image/*";
		}

		if(checkSuffix(fileName, RongContext.getInstance().getResources().getStringArray(io.rong.imkit.R.array.rc_file_file_suffix))) {
			intent.addCategory("android.intent.category.DEFAULT");
			intent.addFlags(268435456);
			type = "text/plain";
		}

		if(checkSuffix(fileName, RongContext.getInstance().getResources().getStringArray(io.rong.imkit.R.array.rc_video_file_suffix))) {
			intent.addFlags(67108864);
			intent.putExtra("oneshot", 0);
			intent.putExtra("configchange", 0);
			type = "video/*";
		}

		if(checkSuffix(fileName, RongContext.getInstance().getResources().getStringArray(io.rong.imkit.R.array.rc_audio_file_suffix))) {
			intent.addFlags(67108864);
			intent.putExtra("oneshot", 0);
			intent.putExtra("configchange", 0);
			type = "audio/*";
		}

		if(checkSuffix(fileName, RongContext.getInstance().getResources().getStringArray(io.rong.imkit.R.array.rc_word_file_suffix))) {
			intent.addCategory("android.intent.category.DEFAULT");
			intent.addFlags(268435456);
			type = "application/msword";
		}

		if(checkSuffix(fileName, RongContext.getInstance().getResources().getStringArray(io.rong.imkit.R.array.rc_excel_file_suffix))) {
			intent.addCategory("android.intent.category.DEFAULT");
			intent.addFlags(268435456);
			type = "application/vnd.ms-excel";
		}

		if(checkSuffix(fileName, RongContext.getInstance().getResources().getStringArray(io.rong.imkit.R.array.rc_pdf_file_suffix))) {
			intent.addCategory("android.intent.category.DEFAULT");
			intent.addFlags(268435456);
			type = "application/pdf";
		}

		if(type != null && fileSavePath != null && isIntentHandlerAvailable(RongContext.getInstance(), intent)) {
			Uri uri = FileProvider.getUriForFile(RongContext.getInstance(), context.getPackageName() + context.getResources().getString(io.rong.imkit.R.string.rc_authorities_fileprovider), new File(fileSavePath));
			intent.setDataAndType(uri, type);
			return intent;
		} else {
			return null;
		}
	}

	private static boolean isIntentHandlerAvailable(Context context, Intent intent) {
		PackageManager packageManager = context.getPackageManager();
		List<ResolveInfo> infoList = packageManager.queryIntentActivities(intent, 65536);
		return infoList.size() > 0;
	}

	private static boolean checkSuffix(String fileName, String[] fileSuffix) {
		String[] var2 = fileSuffix;
		int var3 = fileSuffix.length;

		for(int var4 = 0; var4 < var3; ++var4) {
			String suffix = var2[var4];
			if(fileName != null && fileName.toLowerCase().endsWith(suffix)) {
				return true;
			}
		}

		return false;
	}

}
